<?php
	header('content-type:text/xml;charset=utf-8');
	include('admin/func.main.php');

	$conn = new DBConnector(DB_USER,DB_PASS,DB_NAME,DB_HOST);

	$data = array();
	$page_ids = array();
	$host = $_SERVER['HTTP_HOST'];
	$xml = '<?xml version="1.0" encoding="UTF-8"?>
	<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

	global $kvantis;
	$where_id = count( $kvantis ) ? 'AND `id` NOT IN( '.implode( ', ', array_flip( $kvantis ) ).' )' : '';
	
	$conn->query('
		SELECT `id`, `url`, `parents`, date_format(`date_last_edit`, "%Y-%m-%d") AS `date_last_edit` 
			FROM `pages` 
				WHERE `templ` NOT IN( "_main", "_search", "comparison", "order" ) '.$where_id.'
					ORDER BY `parents`');
						
	if($conn->rowCount())
	{
		$i=0;
		while( $row = $conn->fetch())
		{
			$pre_url = '';
			if(!empty($row->parents))
			{
				if(preg_match_all('/#(\d+)#/',$row->parents, $matches))
				{
					if(sizeof($matches[1])>0)
					{
						foreach ($matches[1] as $parent_id) {
							if(!empty($data[$parent_id]))
								$pre_url .='/'.$data[$parent_id];
						}
					}
				}
			}
			$url = $host.$pre_url .'/'. $row->url;
			$xml .= '<url>
		  <loc>http://'.$url.'</loc>
		  <lastmod>'.$row->date_last_edit.'</lastmod>
		  <changefreq>weekly</changefreq>
		  <priority>0.8</priority>
	   </url>';
			$data[$row->id] = $row->url;
		}

	}
	$xml.='</urlset>';
	echo $xml;
